package com.iwhalecloud.bss.kite.cucc.listener.prod.local;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.listener.handler.LocalProdInstFuncHandler;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalQuerySeqHelper;
import com.iwhalecloud.bss.kite.event.prod.AddProdFuncEvent;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;

/**
 * 新增功能产品事件监听器
 *
 * @author wei.yanhong
 */

@Component
public class LocalAddProdFuncListener {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    /**
     * 新增功能产品，联动处理其他功能产品属性
     *
     * @param event
     */
    @EventListener
    @Order(4)
    @Async
    public void registerFuncProdDeal(AddProdFuncEvent event) {
        if (event.isOldInst()) {
            return;
        }
        String custId = event.getCustId();

        KiteProdInstFunc kiteProdInstFunc = querySceneInstService.getProdInstFunc(custId, event.getFuncProdInstId());
        KiteProdInst kiteProdInst = querySceneInstService.getProdInst(custId, event.getProdInstId());
        if (kiteProdInst == null || kiteProdInstFunc == null) {
            return;
        }

    }

    
    /**
     * 新增功能产品，联动处理其他功能产品属性
     *
     * @param event
     */
    @EventListener
    @Order(5)
    @Async
    public void generateAttrGroupInstId(AddProdFuncEvent event) {
        if (event.isOldInst()) {
            return;
        }
        String prodInstId = event.getProdInstId();
        String sceneInstId = event.getSceneInstId();
        String custId = event.getCustId();
        
        KiteSceneInst kiteSceneInst = this.querySceneInstService.getSceneInst(sceneInstId);
        String askSource = kiteSceneInst.getAskSource();
        if (KiteStringUtils.equalsAny(askSource, LocalKeyConsts.ASK_SOURCE_ORDER, LocalKeyConsts.ASK_SOURCE_USER)) {
            return;
        }
        CuccContextCustDTO custDTO = new CuccContextCustDTO();
        custDTO.setProvinceCode(kiteSceneInst.getProvinceNbr());
        custDTO.setEparchyCode(kiteSceneInst.getLanId());
        custDTO.setRegionId(kiteSceneInst.getRegionId());

        // 生成新的属性组实例ID
        Map<String, String> inParam = new HashMap<>();
        inParam.put("SEQ_TYPE", "SERVICE_ATTR_GROUP_ITEM_ID");
        inParam.put("SYS_TYPE", "1");
        inParam.put("SYS_SOURCE", "");
        String attrGroupInstId = LocalQuerySeqHelper.getSeqBySeqType(inParam, sceneInstId, prodInstId, custDTO);
        KiteProdInstFunc prodInstFunc = this.querySceneInstService.getProdInstFunc(custId, event.getFuncProdInstId());
        if (!KiteObjectUtils.isEmpty(prodInstFunc)) {
            KiteAttr kiteAttr = new KiteAttr();
            kiteAttr.setFieldName(LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + prodInstFunc.getSpecId());
            kiteAttr.setValue(attrGroupInstId);
            kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            prodInstFunc.setAttr(kiteAttr);
            this.operateSceneInstService.addInst(sceneInstId, prodInstFunc);
        }
    }
}
